package cn.hniu.controller;

import cn.hniu.mapper.TopicsMapper;
import cn.hniu.pojo.TextPosts;
import cn.hniu.pojo.Topics;
import cn.hniu.pojo.util.PageAll;
import cn.hniu.pojo.util.Result;
import cn.hniu.service.TopicsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/topics")
public class TopicsController {
    @Autowired
    private TopicsService topicsService;
    @Autowired
    private TopicsMapper topicsMapper;

    //添加话题
    @PostMapping("/addTopics")
    @ResponseBody
    public Result addTopics(@RequestBody Topics topics){
        List<Topics> topicsList=topicsMapper.selectList(null);
        int num=0;
        for(Topics topic:topicsList){
            if(topic.getT_name().equals(topics.getT_name())){
                num++;
            }
        }
        if(num>0){
            return new Result(205,"标题出现重复");
        }else
        if(topicsService.addTopics(topics)>0){
            return new Result(200,"添加成功");
        }else
        {
            return new Result(400,"添加失败");
        }
    }

    //查询话题
    @GetMapping("/{data}/{begin}/{size}")
    @ResponseBody
    public Result findTextPosts(@PathVariable("data") String data,
                                @PathVariable("begin") Integer begin,
                                @PathVariable("size") Integer size){
        if(topicsService.findTopics(data,begin-1,size).size()==0){
            return new Result(400,"没有查到");
        }else {
            int list = topicsMapper.selectCount(new QueryWrapper<Topics>().like("t_name", data));
            PageAll pageAll=new PageAll();
            pageAll.setCurrent(begin);
            pageAll.setPages((int)Math.ceil((double)list/size));
            pageAll.setSize(size);
            pageAll.setTotals(list);
            pageAll.setTopicsList(topicsService.findTopics(data,begin-1,size));
            return new Result(200,pageAll);
        }
    }

    //修改话题
    @PutMapping("/updateTopics")
    @ResponseBody
    public Result updateTopics(@RequestBody Topics topics){
        if(topicsService.updateTopics(topics)>0){
            return new Result(200,"修改成功");
        }else
        {
            return new Result(400,"修改失败");
        }
    }

    //删除话题
    @PutMapping("/deleteTopics/{id}")
    @ResponseBody
    public Result deleteTopics(@PathVariable("id") Integer id){
        if(topicsService.deleteTopics(id)>0){
            return new Result(200,"删除成功");
        }else
        {
            return new Result(400,"删除失败");
        }
    }

    //查询话题热度排行 热度排序为前十的记录
    @GetMapping("/findTopicsByRank")
    public Result findTopicsByRank(){
        if(topicsService.findTopicsByRank().size()==0){
            return new Result(400,"没查到");
        }else {
            return new Result(200,topicsService.findTopicsByRank());
        }
    }
}
